<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function a(num,n) {
            
            var re = '\\d(?=(\\d{3})+' + (n > 0 ? '\\.' : '$') + ')';
            return num.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,').toString();
        };
        var ans=a(31111,2)
        console.log(ans)
        console.log(typeof(ans))
    </script>
</body>

</html>